package com.grf.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @description: 用户名密码认证
 * @author: guorf
 * @date: 2024/8/6
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        // http
        //         .authorizeHttpRequests((authorize) -> authorize
        //                 .anyRequest().authenticated()
        //         )
        //         .httpBasic(Customizer.withDefaults())
        //         .formLogin(Customizer.withDefaults());
        http.httpBasic(Customizer.withDefaults());
        return http.build();
    }

    // @Bean
    // public AuthenticationManager authenticationManager(
    //         UserDetailsService userDetailsService,
    //         PasswordEncoder passwordEncoder) {
    //     DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
    //     authenticationProvider.setUserDetailsService(userDetailsService);
    //     authenticationProvider.setPasswordEncoder(passwordEncoder);
    //
    //     return new ProviderManager(authenticationProvider);
    // }

    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails userDetails = User.withDefaultPasswordEncoder()
                .username("grf")
                .password("123")
                .roles("USER")
                .build();

        return new InMemoryUserDetailsManager(userDetails);
    }
}
